package com.jiajun.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {

	private static DB db = new DB();
	private Connection conn = null;

	private DB() {

	}

	public static DB getDefault() {
		return db;
	}

	public Connection getConn() {
		if (conn == null) {
			try {
				Class.forName("org.sqlite.JDBC");
				conn = DriverManager.getConnection("jdbc:sqlite:db/sqlite.db");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return conn;
	}

	public static void main(String[] args) throws Exception {
		Class.forName("org.sqlite.JDBC");
		Connection conn = DriverManager
				.getConnection("jdbc:sqlite:db/sqlite.db");
		Statement stat = conn.createStatement();
		// stat.executeUpdate("drop table if exists ;");
		// stat.executeUpdate("create table people (name, occupation);");
		// PreparedStatement prep = conn
		// .prepareStatement("insert into people values (?, ?);");
		//
		// prep.setString(1, "Gandhi");
		// prep.setString(2, "politics");
		// prep.addBatch();
		// prep.setString(1, "Turing");
		// prep.setString(2, "computers");
		// prep.addBatch();
		// prep.setString(1, "Wittgenstein");
		// prep.setString(2, "smartypants");
		// prep.addBatch();
		//
		// conn.setAutoCommit(false);
		// prep.executeBatch();
		// conn.setAutoCommit(true);

		ResultSet rs = stat.executeQuery("select * from user;");
		while (rs.next()) {
			System.out.println("user_id = " + rs.getString("user_id"));
			System.out.println("user_name = " + rs.getString("user_name"));
		}
		rs.close();
		conn.close();
	}
}
